#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "defines.h"
#include "tree_t.cpp"

int main(int, char *argv[])
{
	OUT("# %s \n"
		"# (%s)\n"
		"# %s %s:\n\n",
		argv[0], __FILE__, __DATE__, __TIME__);

    printf("Tree builder test.\n");
	printf("Using:\t'TREE' program_file_to_process\n\n");

	char *file_name = NULL;
	if (argv[1] != NULL) file_name = strdup(argv[1]);
	else
	{
		printf("No input filename argument, enter the name of the file with the expression\n");
		file_name = (char*)calloc(255, sizeof(char));
		assert(file_name != NULL);
		scanf("%s", file_name);
	}

    FILE *f = fopen (file_name, "r");
    assert (f != NULL);
    if (file_name != NULL) free(file_name);

    char *memory = (char *)calloc(128, sizeof(char));
    assert (memory != NULL);

    unsigned i = 0;
    while (fscanf (f, "%c", &memory[i]) != EOF) i++;
    fclose (f);

    unsigned int idx = 0;
    struct tree_elem_t *my_tree = tree_read (memory, &idx, IN_ORDER);

    free (memory);

    f = fopen ("result.txt", "w");
    tree_print (my_tree, f, POST_ORDER);
    fclose(f);

    tree_destructor (my_tree);

    return 0;
}
